package com.scalar.chiptrack.operations.reports.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.operations.reports.forms.BoardandKitInventoryReportForm;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.users.User;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.util.LabelValueBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Aug 29, 2007
 * Time: 3:55:02 PM
 * To change this template use File | Settings | File Templates.
 */
public class BoardandKitInventoryReportAction extends ChipTrackAction
{
    private static final String ACTION_NAME = "/boardandKitInventoryReportAction.do";

    public ActionForward execute ( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception
    {
        String action = request.getParameter( "action" );

        HttpSession session = request.getSession();
        User user = ( User ) session.getAttribute( SessionKeys.USER_KEY );

        if( user == null )
        {
            session.setAttribute( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString() );
            return ( mapping.findForward( "login" ) );
        }

        try
        {
            BoardandKitInventoryReportForm boardandKitInventoryReportForm = ( BoardandKitInventoryReportForm ) form;

            if ( "boardandKitInventoryReportAction".equalsIgnoreCase(action) || "boardandKitInventorySummaryReport".equalsIgnoreCase(action))
            {
                ArrayList partNumberList    = null;
                ArrayList vendorList        = null;
                String productType          = null;

                productType = request.getParameter( "productType" );

                if ( "boardandKitInventoryReportAction".equalsIgnoreCase(action) )
                {
                    if ( productType == null || "0".equalsIgnoreCase ( productType ) )
                    {
                       partNumberList = new ArrayList();
                    }
                    else
                    {
                        partNumberList = NewProductDAO.getPartNumberbyProductType( null, productType,"0" );
                    }
                }
                else
                {
                     partNumberList = NewProductDAO.getPartNumberbyProductType( null, "0","0" );
                }

                vendorList = VendorDAO.getAllVendorNamesByUserType( null, user );

                request.setAttribute ( "vendorList" , vendorList );
                request.setAttribute ( "partNumberList", partNumberList );
                request.setAttribute ( "categoryList", getLotCategory () );
                request.setAttribute ( "statusList" , NewProductDAO.loadStatusList() );
            }


        }
        catch( Exception e )
        {
            e.printStackTrace();
        }

        if( "boardandKitInventoryReportAction".equalsIgnoreCase(action) )
        {
            return( mapping.findForward( "boardandKitInventoryReportAction" ) ) ;
        }
        else if( "boardandKitInventorySummaryReport".equalsIgnoreCase(action) )
        {
            return( mapping.findForward( "boardandKitInventorySummaryReport" ) ) ;
        }
        else
        return null;
    }

    private ArrayList getLotCategory()
    {
        ArrayList categoryList = new ArrayList();

        categoryList.add( new LabelValueBean( "All Categories", "0") );
        categoryList.add( new LabelValueBean( "PROD", "PROD") );
        categoryList.add( new LabelValueBean( "ENGG", "ENGG" ) );

        return categoryList;
    }
}
